package mamikeji.liaowa.ui.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.List;

import mamikeji.liaowa.R;
import mamikeji.liaowa.bean.EventListBean;

/**
 * Created by 廉冲-android开发 on 2017/6/14.
 */
public class NoticeEventAdapter extends BaseAdapter {
    public static final int TYPE_LEFT = 0;
    public static final int TYPE_CENTER = 1;
    public static final int TYPE_RIGHT = 2;
    List<EventListBean.DataEntity> mList = new ArrayList<>();
    Context context;

    public boolean bool = false;

    public NoticeEventAdapter() {
    }

    public NoticeEventAdapter(List<EventListBean.DataEntity> mList, Context context) {
        this.mList = mList;
        this.context = context;
    }

    public void setList(List<EventListBean.DataEntity> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public EventListBean.DataEntity getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        EventListBean.DataEntity dataEntity = getItem(position);

        switch (dataEntity.getImgtype()-1) {
            case TYPE_LEFT:
                position = TYPE_LEFT;
                break;
            case TYPE_CENTER:
                position = TYPE_CENTER;
                break;
            case TYPE_RIGHT:
                position = TYPE_RIGHT;
                break;
        }
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        EventListBean.DataEntity dataEntity = getItem(position);

        ViewHolder_left viewHolder_left;
        ViewHolder_center viewHolder_center;
        ViewHolder_right viewHolder_right;
        switch (getItemViewType(position)) {
            case TYPE_LEFT:
                if (convertView == null) {
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_activity_left, null);
                    viewHolder_left = new ViewHolder_left();

                    viewHolder_left.textView_activity_left_title = (TextView) convertView.findViewById(R.id.textView_activity_left_title);
                    viewHolder_left.textView_activity_left_content = (TextView) convertView.findViewById(R.id.textView_activity_left_content);
                    viewHolder_left.imageView_activity_left = (SimpleDraweeView) convertView.findViewById(R.id.imageView_activity_left);
                    viewHolder_left.textView_activity_left_time = (TextView) convertView.findViewById(R.id.textView_activity_left_time);

                    convertView.setTag(viewHolder_left);
                } else {
                    viewHolder_left = (ViewHolder_left) convertView.getTag();
                }

                viewHolder_left.textView_activity_left_content.setVisibility(!bool ? View.VISIBLE : View.GONE);
                viewHolder_left.textView_activity_left_time.setVisibility(bool ? View.VISIBLE : View.GONE);

                viewHolder_left.textView_activity_left_title.setText(dataEntity.getTitle());
                viewHolder_left.textView_activity_left_content.setText(dataEntity.getContent());
                viewHolder_left.textView_activity_left_time.setText(dataEntity.getPublishdate());

                viewHolder_left.imageView_activity_left.setImageURI(Uri.parse(dataEntity.getImgurl()));
                break;
            case TYPE_CENTER:
                if (convertView == null) {
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_activity_center, null);
                    viewHolder_center = new ViewHolder_center();

                    viewHolder_center.imageView_activity_center = (SimpleDraweeView) convertView.findViewById(R.id.imageView_activity_center);

                    convertView.setTag(viewHolder_center);
                } else {
                    viewHolder_center = (ViewHolder_center) convertView.getTag();
                }

                viewHolder_center.imageView_activity_center.setImageURI(Uri.parse(dataEntity.getImgurl()));
                break;
            case TYPE_RIGHT:
                if (convertView == null) {
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_activity_right, null);
                    viewHolder_right = new ViewHolder_right();

                    viewHolder_right.textView_activity_right_title = (TextView) convertView.findViewById(R.id.textView_activity_right_title);
                    viewHolder_right.textView_activity_right_content = (TextView) convertView.findViewById(R.id.textView_activity_right_content);
                    viewHolder_right.imageView_activity_right = (SimpleDraweeView) convertView.findViewById(R.id.imageView_activity_right);

                    convertView.setTag(viewHolder_right);
                } else {
                    viewHolder_right = (ViewHolder_right) convertView.getTag();
                }

                viewHolder_right.textView_activity_right_title.setText(dataEntity.getTitle());
                viewHolder_right.textView_activity_right_content.setText(dataEntity.getContent());

                viewHolder_right.imageView_activity_right.setImageURI(Uri.parse(dataEntity.getImgurl()));
                break;
        }
        return convertView;
    }

    public class ViewHolder_left {
        TextView textView_activity_left_time;
        TextView textView_activity_left_title;
        TextView textView_activity_left_content;
        SimpleDraweeView imageView_activity_left;
    }

    public class ViewHolder_center {
        SimpleDraweeView imageView_activity_center;
    }

    public class ViewHolder_right {
        TextView textView_activity_right_title;
        TextView textView_activity_right_content;
        SimpleDraweeView imageView_activity_right;
    }
}
